package com.amarsoft.are.lang;

import com.amarsoft.are.ARE;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Enumeration;

public class ObjectX
{
  public static boolean setProperty(Object paramObject, String paramString, int paramInt, boolean paramBoolean)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString, Integer.TYPE, new Integer(paramInt), paramBoolean);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean setProperty(Object paramObject, String paramString, long paramLong, boolean paramBoolean)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString, Long.TYPE, new Long(paramLong), paramBoolean);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean setProperty(Object paramObject, String paramString, double paramDouble, boolean paramBoolean)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString, Double.TYPE, new Double(paramDouble), paramBoolean);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean setProperty(Object paramObject, String paramString, char paramChar, boolean paramBoolean)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString, Character.TYPE, new Character(paramChar), paramBoolean);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean setProperty(Object paramObject, String paramString, byte paramByte, boolean paramBoolean)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString, Character.TYPE, new Byte(paramByte), paramBoolean);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean setProperty(Object paramObject, String paramString, boolean paramBoolean1, boolean paramBoolean2)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString, Boolean.TYPE, Boolean.valueOf(paramBoolean1), paramBoolean2);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean setProperty(Object paramObject, String paramString, Date paramDate, boolean paramBoolean)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString, Date.class, paramDate, paramBoolean);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static boolean setProperty(Object paramObject, String paramString1, String paramString2, boolean paramBoolean)
  {
    if (paramObject == null)
      return false;
    try
    {
      setObjectProperty(paramObject, paramString1, String.class, paramString2, paramBoolean);
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  private static void setObjectProperty(Object paramObject1, String paramString, Class<?> paramClass, Object paramObject2, boolean paramBoolean)
    throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
  {
    String str1 = null;
    Class<?> localClass = paramObject1.getClass();
    if (paramBoolean)
    {
      if (!(paramString.startsWith(localClass.getName() + ".")))
        throw new IllegalArgumentException("Class not match: " + paramString);
      str1 = paramString.substring(localClass.getName().length() + 1);
    }
    else
    {
      str1 = paramString;
    }
    String str2 = "set" + str1.substring(0, 1).toUpperCase() + str1.substring(1);
    Method localMethod = localClass.getMethod(str2, new Class[] { paramClass });
    localMethod.invoke(paramObject1, new Object[] { paramObject2 });
  }

  public static boolean setPropertyX(Object paramObject, String paramString1, String paramString2, boolean paramBoolean)
  {
    if (paramBoolean)
    {
      String str = paramObject.getClass().getName() + ".";
      if (!(paramString1.startsWith(str)))
        return false;
    }
    try
    {
      setObjectProperty(paramObject, paramString1, String.class, paramString2, paramBoolean);
      return true;
    }
    catch (Exception localException8)
    {
      try
      {
        setObjectProperty(paramObject, paramString1, Date.class, StringX.parseDate(paramString2), paramBoolean);
        return true;
      }
      catch (Exception localException1)
      {
        try
        {
          setObjectProperty(paramObject, paramString1, Boolean.TYPE, new Boolean(StringX.parseBoolean(paramString2)), paramBoolean);
          return true;
        }
        catch (Exception localException3)
        {
          if ((paramString2 == null) || (paramString2.equals("")))
            return false;
        }
        try
        {
          setObjectProperty(paramObject, paramString1, Integer.TYPE, Integer.valueOf(paramString2), paramBoolean);
          return true;
        }
        catch (Exception localException2)
        {
          try
          {
            setObjectProperty(paramObject, paramString1, Long.TYPE, Long.valueOf(paramString2), paramBoolean);
            return true;
          }
          catch (Exception localException3)
          {
            try
            {
              setObjectProperty(paramObject, paramString1, Byte.TYPE, Byte.valueOf(paramString2), paramBoolean);
              return true;
            }
            catch (Exception localException4)
            {
              try
              {
                setObjectProperty(paramObject, paramString1, Character.TYPE, new Character(paramString2.charAt(0)), paramBoolean);
                return true;
              }
              catch (Exception localException5)
              {
                try
                {
                  setObjectProperty(paramObject, paramString1, Double.TYPE, Double.valueOf(paramString2), paramBoolean);
                  return true;
                }
                catch (Exception localException6)
                {
                }
              }
            }
          }
        }
      }
    }
    return false;
  }

  public static void setPropertyX(Object paramObject, Element paramElement)
  {
    Object localObject = paramElement.getValue();
    try
    {
      setObjectProperty(paramObject, paramElement.getName(), localObject.getClass(), localObject, false);
    }
    catch (Exception localException)
    {
      ARE.getLog().debug(localException);
    }
  }

  public static void setProperties(Object paramObject, java.util.Properties paramProperties, boolean paramBoolean)
  {
    Enumeration<?> localEnumeration = paramProperties.keys();
    while (localEnumeration.hasMoreElements())
    {
      String str1 = (String)localEnumeration.nextElement();
      String str2 = paramProperties.getProperty(str1);
      setPropertyX(paramObject, str1, str2, paramBoolean);
    }
  }

  public static Object createObject(String paramString)
    throws Exception
  {
    Object localObject = null;
    Class<?> localClass = null;
    if (StringX.isSpace(paramString))
      throw new Exception("21");
    try
    {
      localClass = Class.forName(StringX.trimAll(paramString));
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      ARE.getLog().debug("Create object Failed, Class not found!", localClassNotFoundException);
      throw new Exception(localClassNotFoundException);
    }
    try
    {
      localObject = localClass.newInstance();
    }
    catch (InstantiationException localInstantiationException)
    {
      ARE.getLog().debug("Create object Failed, construct Object error!", localInstantiationException);
      throw new Exception(localInstantiationException);
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      ARE.getLog().debug("Create object Failed, illegal access!", localIllegalAccessException);
      throw new Exception(localIllegalAccessException);
    }
    return localObject;
  }

  public static boolean equals(Object paramObject1, Object paramObject2)
  {
    if ((paramObject1 == null) && (paramObject2 == null))
      return true;
    if (((paramObject1 == null) && (paramObject2 != null)) || ((paramObject2 == null) && (paramObject1 != null)))
      return false;
    return paramObject1.equals(paramObject2);
  }
}